package top.xana.acg.music.ui.page

import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import top.xana.acg.common.base.ui.BaseFragment
import top.xana.acg.music.R
import top.xana.acg.music.databinding.MainFragmentBinding

class MainFragment : BaseFragment() {

    companion object {
        fun newInstance() = MainFragment()
    }

    private val b by lazy { getBinding<MainFragmentBinding>() }

    private lateinit var viewModel: MainViewModel
    override fun getLayoutId() = R.layout.main_fragment
    override fun initView() {
        super.initView()

        b.container.fitsSystemWindows = true
        setTabViewPager()
    }

    private fun setTabViewPager(){
        val mainArray = resources.getStringArray(R.array.mainArray)
        TabLayoutMediator(b.tabLayout, b.viewPager2.apply {adapter = MainAdapter(requireFragmentManager(), lifecycle, mainArray)}) { tab, position ->
            tab.text = mainArray[position]
        }.attach()
}

    class MainAdapter(fm: FragmentManager, lc: Lifecycle, val mainArray: Array<String>):
        FragmentStateAdapter(fm, lc){
        override fun getItemCount() = mainArray.size

        override fun createFragment(position: Int)  = FindFragment.newInstance()

    }

}